﻿--Inport: PMEngine, PrivateDB, ProfileDB, GlobalDB
local PMEngine, L, PrivateDB, ProfileDB, GlobalDB = unpack(select(2, ...)); 
local CB = PMEngine:GetModule('CastBars')

local MODNAME = "Focus"
local Focus = PMEngine:NewModule(MODNAME, "AceEvent-3.0")
CB.Focus = Focus

----------------------------
-- Upvalues
local UnitIsEnemy, UnitIsFriend, UnitIsUnit = UnitIsEnemy, UnitIsFriend, UnitIsUnit

local db, getOptions

function Focus:Initialize()
	self.db = CB:Merge(PMEngine.db.castbars.CastBarTemplateDefaults, PMEngine.db.castbars.focus)
	db = self.db
	
	self:SetEnabledState(Focus)
	
	self.Bar = CB.CastBarTemplate:new(self, "focus", MODNAME, "Focus", db)

	self:MyEnable()
end

function Focus:MyEnable()
	self.Bar:RegisterEvents()
	self.Bar:RegisterEvent("PLAYER_TARGET_CHANGED")
	self.Bar:RegisterEvent("PLAYER_FOCUS_CHANGED")
	self.Bar.PLAYER_TARGET_CHANGED = self.Bar.UpdateUnit
	self.Bar.PLAYER_FOCUS_CHANGED = self.Bar.UpdateUnit
	self.lastNotInterruptible = false
	self:ApplySettings()
end

function Focus:MyDisable()
	self.Bar:UnregisterEvents()
	self.Bar:Hide()
end

function Focus:PreShowCondition(bar, unit)
	if (not db.showfriendly and UnitIsFriend("player", unit)) or 
	   (not db.showhostile and UnitIsEnemy("player", unit)) or
	   (not db.showtarget and UnitIsUnit("target", unit)) then
		return true
	end
end

function Focus:ApplySettings()
	db = self.db
	
	self.Bar:SetConfig(db)
	if self:IsEnabled() then
		self.Bar:ApplySettings()
	end
end

function Focus:Unlock()
	self.Bar:Unlock()
end

function Focus:Lock()
	self.Bar:Lock()
end